import api from '../../../api/index'
import dataTools from '../../../utils/dateTools'
import obj2Arr from '../../../utils/obj2Arr'
Page({
    pageName: '饮食推荐/首页',
    data: {
        foodPatternIcon: [
            {
                name: '蔬菜类',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_sc.png'
            },
            {
                name: '坚果类',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_jg.png'
            },
            {
                name: '奶类',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_nn.png'
            },
            {
                name: '植物油',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_zwy.png'
            },
            {
                name: '水果类',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_sg.png'
            },
            {
                name: '谷暑类',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_mls.png'
            },
            {
                name: '豆鱼蛋肉',
                icon: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/icon_rou.png'
            }
        ],
        bgHexagon:
            'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon/bg-hexagon.png',
        kcalTargetIcon: {
            dbz: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon-kcaltarget/dbz.png',
            rl: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon-kcaltarget/rl.png',
            ts: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon-kcaltarget/ts.png',
            zf: 'https://hxhealth.oss-cn-beijing.aliyuncs.com/redstart/intelligent-recommend/food/icon-kcaltarget/zf.png'
        },
        totalData: [],
        active: 0, // 默认为早餐
        breakfast: [],
        lunch: [],
        dinner: [],
        calanderDate: [],
        nutrTarget: {},
        foodStrutre: {}
    },
    onChange (event) {},
    toDetail (e) {
        const item = e.currentTarget.dataset.item
        console.log(item)
        wx.navigateTo({
            url: `/package-recommend/pages/food-detail/index?name=${encodeURIComponent(
                item.name
            )}`
        })
    },

    onLoad (options) {
        // this.fromHome = options.fromHome // 记录页面来源
        this.getTicket()
    },
    async getTicket() {
        try {
            // await app.isLogin({ctx: this})
            const list = await api.post('/api/hxhealth/user/laikangticket')
            console.log(list);
            this.getRecipes(list[0].url)
            this.getStrutre(list[0].url)
        console.log('haha----------------');
            return list
        } catch (e) {
            return null
        }
    },
    
    dataHandler (data) {
        const dataContent = JSON.parse(data.data)
        console.log(dataContent, '2.获取大json')
        // 1. 今日营养目标 在内层取随着日期的变化而变化默认取第 0 个
        const nutrTarget = dataContent.recipts[0].recipe.nutritious
        // 2. 食谱的数组
        const recipts = dataContent.recipts
        // 3. 添加周几，只保留日期
        let t = recipts.map(item => {
            return {
                ...item,
                day: item.date.split('-')[2],
                week: dataTools.getWeek(item.date),
                totalBreakfast: obj2Arr.handleBreakfast(item.recipe.breakfast),
                totalLunch: obj2Arr.handleLunch(item.recipe.lunch),
                totalDinner: obj2Arr.handleDinner(item.recipe.dinner)
            }
        })
        console.log(t, '格式化早午晚餐')
        let calanderDate = t.map(item => {
            return {
                date: item.date,
                day: item.day,
                week: item.week
            }
        })
        // 4. 初始化早餐数据
        const breakfast = t[0].totalBreakfast
        // 5. 初始化午餐数据
        const lunch = t[0].totalLunch
        // 6.初始化晚餐数据
        const dinner = t[0].totalDinner

        this.setData({
            totalData: t,
            calanderDate,
            nutrTarget,
            breakfast,
            lunch,
            dinner
        })
    },
    onDateChange (v) {
        // console.log(v,"日期发生变化")
        const date = v.detail
        // 根据日期获取当前日的对象从而实现动态加载数据
        const activeObj = this.data.totalData.find(item => item.date === date)
        // console.log(activeObj,"选中日期的当前对象")
        const nutrTarget = activeObj.recipe.nutritious
        const breakfast = activeObj.totalBreakfast
        const lunch = activeObj.totalLunch
        const dinner = activeObj.totalDinner
        this.setData({
            nutrTarget,
            breakfast,
            lunch,
            dinner
        })
    },
    strutreHandler (strutre) {
        // console.log("====入参====",strutre)
        const dataContent = JSON.parse(strutre.data)
        // console.log("获取大JSON",dataContent)
        // 将数组转成对象
        let retStrutre = {}
        dataContent.foodComponent.map(item => {
            retStrutre[`${item.k}`] = item.v
        })
        console.log('饮食结构', retStrutre)
        this.setData({
            foodStrutre: retStrutre
        })
    },
    getRecipes (name) {
        wx.showLoading({
            title: '数据加载中...'
        })
        const url = `${name}/api/health/recommend/recipes?`
        api.adminGet(url)
            .then(res => {
                wx.hideLoading()
                this.dataHandler(res[0])
            })
            .catch(e => {
                console.log(e)
                wx.hideLoading()
                wx.showToast({
                    title: e.faultstring,
                    icon: 'none'
                })
            })
    },
    getStrutre (name) {
        const url = `${name}/api/health/recommend/cookbook/strutre?`
        api.adminGet(url)
            .then(res => {
                this.strutreHandler(res[0])
            })
            .catch(e => {
                console.log(e)
            })
    }
})
